<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title>API: animation   AnimMgr.js  (YUI Library)</title>
	<link rel="stylesheet" type="text/css" href="assets/api.css">
</head>

<body id="yahoo-com">
<div id="doc3" class="yui-t2">

	<div id="hd">
        <h1>Yahoo! UI Library</h1>
        <h3>animation&nbsp; <span class="subtitle">2.2.2</span></h3>
        <p>
        <a href="./index.html">Yahoo! UI Library</a> 
            &gt; <a href="./module_animation.html">animation</a>
                
                 &gt; AnimMgr.js (source view) 
            </p>
	</div>

	<div id="bd">
		<div id="yui-main">
			<div class="yui-b">

                    <div id="srcout">
<div class="highlight" ><pre><span class="c">/**</span>
<span class="c"> * Handles animation queueing and threading.</span>
<span class="c"> * Used by Anim and subclasses.</span>
<span class="c"> * @class AnimMgr</span>
<span class="c"> * @namespace YAHOO.util</span>
<span class="c"> */</span>
<span class="nx">YAHOO</span><span class="o">.</span><span class="nx">util</span><span class="o">.</span><span class="nx">AnimMgr</span> <span class="o">=</span> <span class="k">new</span> <span class="k">function</span><span class="o">()</span> <span class="o">{</span>
    <span class="c">/** </span>
<span class="c">     * Reference to the animation Interval.</span>
<span class="c">     * @property thread</span>
<span class="c">     * @private</span>
<span class="c">     * @type Int</span>
<span class="c">     */</span>
    <span class="k">var</span> <span class="nx">thread</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
    
    <span class="c">/** </span>
<span class="c">     * The current queue of registered animation objects.</span>
<span class="c">     * @property queue</span>
<span class="c">     * @private</span>
<span class="c">     * @type Array</span>
<span class="c">     */</span>    
    <span class="k">var</span> <span class="nx">queue</span> <span class="o">=</span> <span class="o">[];</span>

    <span class="c">/** </span>
<span class="c">     * The number of active animations.</span>
<span class="c">     * @property tweenCount</span>
<span class="c">     * @private</span>
<span class="c">     * @type Int</span>
<span class="c">     */</span>        
    <span class="k">var</span> <span class="nx">tweenCount</span> <span class="o">=</span> <span class="m">0</span><span class="o">;</span>

    <span class="c">/** </span>
<span class="c">     * Base frame rate (frames per second). </span>
<span class="c">     * Arbitrarily high for better x-browser calibration (slower browsers drop more frames).</span>
<span class="c">     * @property fps</span>
<span class="c">     * @type Int</span>
<span class="c">     * </span>
<span class="c">     */</span>
    <span class="k">this</span><span class="o">.</span><span class="nx">fps</span> <span class="o">=</span> <span class="m">1000</span><span class="o">;</span>

    <span class="c">/** </span>
<span class="c">     * Interval delay in milliseconds, defaults to fastest possible.</span>
<span class="c">     * @property delay</span>
<span class="c">     * @type Int</span>
<span class="c">     * </span>
<span class="c">     */</span>
    <span class="k">this</span><span class="o">.</span><span class="nx">delay</span> <span class="o">=</span> <span class="m">1</span><span class="o">;</span>

    <span class="c">/**</span>
<span class="c">     * Adds an animation instance to the animation queue.</span>
<span class="c">     * All animation instances must be registered in order to animate.</span>
<span class="c">     * @method registerElement</span>
<span class="c">     * @param {object} tween The Anim instance to be be registered</span>
<span class="c">     */</span>
    <span class="k">this</span><span class="o">.</span><span class="nx">registerElement</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">tween</span><span class="o">)</span> <span class="o">{</span>
        <span class="nx">queue</span><span class="o">[</span><span class="nx">queue</span><span class="o">.</span><span class="nx">length</span><span class="o">]</span> <span class="o">=</span> <span class="nx">tween</span><span class="o">;</span>
        <span class="nx">tweenCount</span> <span class="o">+=</span> <span class="m">1</span><span class="o">;</span>
        <span class="nx">tween</span><span class="o">.</span><span class="nx">_onStart</span><span class="o">.</span><span class="nx">fire</span><span class="o">();</span>
        <span class="k">this</span><span class="o">.</span><span class="nx">start</span><span class="o">();</span>
    <span class="o">};</span>
    
    <span class="c">/**</span>
<span class="c">     * removes an animation instance from the animation queue.</span>
<span class="c">     * All animation instances must be registered in order to animate.</span>
<span class="c">     * @method unRegister</span>
<span class="c">     * @param {object} tween The Anim instance to be be registered</span>
<span class="c">     * @param {Int} index The index of the Anim instance</span>
<span class="c">     * @private</span>
<span class="c">     */</span>
    <span class="k">this</span><span class="o">.</span><span class="nx">unRegister</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">tween</span><span class="o">,</span> <span class="nx">index</span><span class="o">)</span> <span class="o">{</span>
        <span class="nx">tween</span><span class="o">.</span><span class="nx">_onComplete</span><span class="o">.</span><span class="nx">fire</span><span class="o">();</span>
        <span class="nx">index</span> <span class="o">=</span> <span class="nx">index</span> <span class="o">||</span> <span class="nx">getIndex</span><span class="o">(</span><span class="nx">tween</span><span class="o">);</span>
        <span class="k">if</span> <span class="o">(</span><span class="nx">index</span> <span class="o">!=</span> <span class="o">-</span><span class="m">1</span><span class="o">)</span> <span class="o">{</span>
            <span class="nx">queue</span><span class="o">.</span><span class="nx">splice</span><span class="o">(</span><span class="nx">index</span><span class="o">,</span> <span class="m">1</span><span class="o">);</span>
        <span class="o">}</span>
        
        <span class="nx">tweenCount</span> <span class="o">-=</span> <span class="m">1</span><span class="o">;</span>
        <span class="k">if</span> <span class="o">(</span><span class="nx">tweenCount</span> <span class="o">&lt;=</span> <span class="m">0</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">this</span><span class="o">.</span><span class="nx">stop</span><span class="o">();</span>
        <span class="o">}</span>
    <span class="o">};</span>
    
    <span class="c">/**</span>
<span class="c">     * Starts the animation thread.</span>
<span class="c">	* Only one thread can run at a time.</span>
<span class="c">     * @method start</span>
<span class="c">     */</span>    
    <span class="k">this</span><span class="o">.</span><span class="nx">start</span> <span class="o">=</span> <span class="k">function</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">if</span> <span class="o">(</span><span class="nx">thread</span> <span class="o">===</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
            <span class="nx">thread</span> <span class="o">=</span> <span class="nx">setInterval</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="nx">run</span><span class="o">,</span> <span class="k">this</span><span class="o">.</span><span class="nx">delay</span><span class="o">);</span>
        <span class="o">}</span>
    <span class="o">};</span>

    <span class="c">/**</span>
<span class="c">     * Stops the animation thread or a specific animation instance.</span>
<span class="c">     * @method stop</span>
<span class="c">     * @param {object} tween A specific Anim instance to stop (optional)</span>
<span class="c">     * If no instance given, Manager stops thread and all animations.</span>
<span class="c">     */</span>    
    <span class="k">this</span><span class="o">.</span><span class="nx">stop</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">tween</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">if</span> <span class="o">(!</span><span class="nx">tween</span><span class="o">)</span> <span class="o">{</span>
            <span class="nx">clearInterval</span><span class="o">(</span><span class="nx">thread</span><span class="o">);</span>
            
            <span class="k">for</span> <span class="o">(</span><span class="k">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="m">0</span><span class="o">,</span> <span class="nx">len</span> <span class="o">=</span> <span class="nx">queue</span><span class="o">.</span><span class="nx">length</span><span class="o">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">len</span><span class="o">;</span> <span class="o">++</span><span class="nx">i</span><span class="o">)</span> <span class="o">{</span>
                <span class="k">if</span> <span class="o">(</span> <span class="nx">queue</span><span class="o">[</span><span class="m">0</span><span class="o">].</span><span class="nx">isAnimated</span><span class="o">()</span> <span class="o">)</span> <span class="o">{</span>
                    <span class="k">this</span><span class="o">.</span><span class="nx">unRegister</span><span class="o">(</span><span class="nx">queue</span><span class="o">[</span><span class="m">0</span><span class="o">],</span> <span class="m">0</span><span class="o">);</span>  
                <span class="o">}</span>
            <span class="o">}</span>

            <span class="nx">queue</span> <span class="o">=</span> <span class="o">[];</span>
            <span class="nx">thread</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
            <span class="nx">tweenCount</span> <span class="o">=</span> <span class="m">0</span><span class="o">;</span>
        <span class="o">}</span>
        <span class="k">else</span> <span class="o">{</span>
            <span class="k">this</span><span class="o">.</span><span class="nx">unRegister</span><span class="o">(</span><span class="nx">tween</span><span class="o">);</span>
        <span class="o">}</span>
    <span class="o">};</span>
    
    <span class="c">/**</span>
<span class="c">     * Called per Interval to handle each animation frame.</span>
<span class="c">     * @method run</span>
<span class="c">     */</span>    
    <span class="k">this</span><span class="o">.</span><span class="nx">run</span> <span class="o">=</span> <span class="k">function</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">for</span> <span class="o">(</span><span class="k">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="m">0</span><span class="o">,</span> <span class="nx">len</span> <span class="o">=</span> <span class="nx">queue</span><span class="o">.</span><span class="nx">length</span><span class="o">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">len</span><span class="o">;</span> <span class="o">++</span><span class="nx">i</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">var</span> <span class="nx">tween</span> <span class="o">=</span> <span class="nx">queue</span><span class="o">[</span><span class="nx">i</span><span class="o">];</span>
            <span class="k">if</span> <span class="o">(</span> <span class="o">!</span><span class="nx">tween</span> <span class="o">||</span> <span class="o">!</span><span class="nx">tween</span><span class="o">.</span><span class="nx">isAnimated</span><span class="o">()</span> <span class="o">)</span> <span class="o">{</span> <span class="k">continue</span><span class="o">;</span> <span class="o">}</span>

            <span class="k">if</span> <span class="o">(</span><span class="nx">tween</span><span class="o">.</span><span class="nx">currentFrame</span> <span class="o">&lt;</span> <span class="nx">tween</span><span class="o">.</span><span class="nx">totalFrames</span> <span class="o">||</span> <span class="nx">tween</span><span class="o">.</span><span class="nx">totalFrames</span> <span class="o">===</span> <span class="kc">null</span><span class="o">)</span>
            <span class="o">{</span>
                <span class="nx">tween</span><span class="o">.</span><span class="nx">currentFrame</span> <span class="o">+=</span> <span class="m">1</span><span class="o">;</span>
                
                <span class="k">if</span> <span class="o">(</span><span class="nx">tween</span><span class="o">.</span><span class="nx">useSeconds</span><span class="o">)</span> <span class="o">{</span>
                    <span class="nx">correctFrame</span><span class="o">(</span><span class="nx">tween</span><span class="o">);</span>
                <span class="o">}</span>
                <span class="nx">tween</span><span class="o">.</span><span class="nx">_onTween</span><span class="o">.</span><span class="nx">fire</span><span class="o">();</span>          
            <span class="o">}</span>
            <span class="k">else</span> <span class="o">{</span> <span class="nx">YAHOO</span><span class="o">.</span><span class="nx">util</span><span class="o">.</span><span class="nx">AnimMgr</span><span class="o">.</span><span class="nx">stop</span><span class="o">(</span><span class="nx">tween</span><span class="o">,</span> <span class="nx">i</span><span class="o">);</span> <span class="o">}</span>
        <span class="o">}</span>
    <span class="o">};</span>
    
    <span class="k">var</span> <span class="nx">getIndex</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">anim</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">for</span> <span class="o">(</span><span class="k">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="m">0</span><span class="o">,</span> <span class="nx">len</span> <span class="o">=</span> <span class="nx">queue</span><span class="o">.</span><span class="nx">length</span><span class="o">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">len</span><span class="o">;</span> <span class="o">++</span><span class="nx">i</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">if</span> <span class="o">(</span><span class="nx">queue</span><span class="o">[</span><span class="nx">i</span><span class="o">]</span> <span class="o">==</span> <span class="nx">anim</span><span class="o">)</span> <span class="o">{</span>
                <span class="k">return</span> <span class="nx">i</span><span class="o">;</span> <span class="c">// note return;</span>
<span class="c"></span>            <span class="o">}</span>
        <span class="o">}</span>
        <span class="k">return</span> <span class="o">-</span><span class="m">1</span><span class="o">;</span>
    <span class="o">};</span>
    
    <span class="c">/**</span>
<span class="c">     * On the fly frame correction to keep animation on time.</span>
<span class="c">     * @method correctFrame</span>
<span class="c">     * @private</span>
<span class="c">     * @param {Object} tween The Anim instance being corrected.</span>
<span class="c">     */</span>
    <span class="k">var</span> <span class="nx">correctFrame</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">tween</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">var</span> <span class="nx">frames</span> <span class="o">=</span> <span class="nx">tween</span><span class="o">.</span><span class="nx">totalFrames</span><span class="o">;</span>
        <span class="k">var</span> <span class="nx">frame</span> <span class="o">=</span> <span class="nx">tween</span><span class="o">.</span><span class="nx">currentFrame</span><span class="o">;</span>
        <span class="k">var</span> <span class="nx">expected</span> <span class="o">=</span> <span class="o">(</span><span class="nx">tween</span><span class="o">.</span><span class="nx">currentFrame</span> <span class="o">*</span> <span class="nx">tween</span><span class="o">.</span><span class="nx">duration</span> <span class="o">*</span> <span class="m">1000</span> <span class="o">/</span> <span class="nx">tween</span><span class="o">.</span><span class="nx">totalFrames</span><span class="o">);</span>
        <span class="k">var</span> <span class="nx">elapsed</span> <span class="o">=</span> <span class="o">(</span><span class="k">new</span> <span class="nb">Date</span><span class="o">()</span> <span class="o">-</span> <span class="nx">tween</span><span class="o">.</span><span class="nx">getStartTime</span><span class="o">());</span>
        <span class="k">var</span> <span class="nx">tweak</span> <span class="o">=</span> <span class="m">0</span><span class="o">;</span>
        
        <span class="k">if</span> <span class="o">(</span><span class="nx">elapsed</span> <span class="o">&lt;</span> <span class="nx">tween</span><span class="o">.</span><span class="nx">duration</span> <span class="o">*</span> <span class="m">1000</span><span class="o">)</span> <span class="o">{</span> <span class="c">// check if falling behind</span>
<span class="c"></span>            <span class="nx">tweak</span> <span class="o">=</span> <span class="nb">Math</span><span class="o">.</span><span class="nx">round</span><span class="o">((</span><span class="nx">elapsed</span> <span class="o">/</span> <span class="nx">expected</span> <span class="o">-</span> <span class="m">1</span><span class="o">)</span> <span class="o">*</span> <span class="nx">tween</span><span class="o">.</span><span class="nx">currentFrame</span><span class="o">);</span>
        <span class="o">}</span> <span class="k">else</span> <span class="o">{</span> <span class="c">// went over duration, so jump to end</span>
<span class="c"></span>            <span class="nx">tweak</span> <span class="o">=</span> <span class="nx">frames</span> <span class="o">-</span> <span class="o">(</span><span class="nx">frame</span> <span class="o">+</span> <span class="m">1</span><span class="o">);</span> 
        <span class="o">}</span>
        <span class="k">if</span> <span class="o">(</span><span class="nx">tweak</span> <span class="o">&gt;</span> <span class="m">0</span> <span class="o">&amp;&amp;</span> <span class="nb">isFinite</span><span class="o">(</span><span class="nx">tweak</span><span class="o">))</span> <span class="o">{</span> <span class="c">// adjust if needed</span>
<span class="c"></span>            <span class="k">if</span> <span class="o">(</span><span class="nx">tween</span><span class="o">.</span><span class="nx">currentFrame</span> <span class="o">+</span> <span class="nx">tweak</span> <span class="o">&gt;=</span> <span class="nx">frames</span><span class="o">)</span> <span class="o">{</span><span class="c">// dont go past last frame</span>
<span class="c"></span>                <span class="nx">tweak</span> <span class="o">=</span> <span class="nx">frames</span> <span class="o">-</span> <span class="o">(</span><span class="nx">frame</span> <span class="o">+</span> <span class="m">1</span><span class="o">);</span>
            <span class="o">}</span>
            
            <span class="nx">tween</span><span class="o">.</span><span class="nx">currentFrame</span> <span class="o">+=</span> <span class="nx">tweak</span><span class="o">;</span>      
        <span class="o">}</span>
    <span class="o">};</span>
<span class="o">};</span>
</pre></div>
                    </div>
			</div>
		</div>
		<div class="yui-b">
            <div class="nav">

                    <div class="module">
                        <h4>Modules</h4>
                        <ul class="content">

                                <li class="selected"><a href="module_animation.html">animation</a></li>

                                <li class=""><a href="module_autocomplete.html">autocomplete</a></li>

                                <li class=""><a href="module_button.html">button</a></li>

                                <li class=""><a href="module_calendar.html">calendar</a></li>

                                <li class=""><a href="module_connection.html">connection</a></li>

                                <li class=""><a href="module_container.html">container</a></li>

                                <li class=""><a href="module_datasource.html">datasource</a></li>

                                <li class=""><a href="module_datatable.html">datatable</a></li>

                                <li class=""><a href="module_dom.html">dom</a></li>

                                <li class=""><a href="module_dragdrop.html">dragdrop</a></li>

                                <li class=""><a href="module_element.html">element</a></li>

                                <li class=""><a href="module_event.html">event</a></li>

                                <li class=""><a href="module_history.html">history</a></li>

                                <li class=""><a href="module_logger.html">logger</a></li>

                                <li class=""><a href="module_menu.html">menu</a></li>

                                <li class=""><a href="module_slider.html">slider</a></li>

                                <li class=""><a href="module_tabview.html">tabview</a></li>

                                <li class=""><a href="module_treeview.html">treeview</a></li>

                                <li class=""><a href="module_yahoo.html">yahoo</a></li>
                        </ul>
                    </div>

                    <div class="module">
                        <h4>Classes</h4>
                        <ul class="content">
                                <li class=""><a href="YAHOO.util.Anim.html">YAHOO.util.Anim</a></li>
                                <li class=""><a href="YAHOO.util.AnimMgr.html">YAHOO.util.AnimMgr</a></li>
                                <li class=""><a href="YAHOO.util.Bezier.html">YAHOO.util.Bezier</a></li>
                                <li class=""><a href="YAHOO.util.ColorAnim.html">YAHOO.util.ColorAnim</a></li>
                                <li class=""><a href="YAHOO.util.Easing.html">YAHOO.util.Easing</a></li>
                                <li class=""><a href="YAHOO.util.Motion.html">YAHOO.util.Motion</a></li>
                                <li class=""><a href="YAHOO.util.Scroll.html">YAHOO.util.Scroll</a></li>
                        </ul>
                    </div>

                    <div class="module">
                        <h4>Files</h4>
                        <ul class="content">        
                                <li class=""><a href="Anim.js.html">Anim.js</a></li>
                                <li class="selected"><a href="AnimMgr.js.html">AnimMgr.js</a></li>
                                <li class=""><a href="Bezier.js.html">Bezier.js</a></li>
                                <li class=""><a href="ColorAnim.js.html">ColorAnim.js</a></li>
                                <li class=""><a href="Easing.js.html">Easing.js</a></li>
                                <li class=""><a href="Motion.js.html">Motion.js</a></li>
                                <li class=""><a href="Scroll.js.html">Scroll.js</a></li>
                        </ul>
                    </div>





            </div>
		</div>
	</div>
	<div id="ft">
        <hr />
        Copyright &copy; 2007 Yahoo! Inc. All rights reserved.
	</div>
</div>
</body>
</html>
